package com.example.football;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;


import android.os.Handler;
import android.os.Message;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.football.dao.PeopleDao;
import com.example.football.entity.People;
import com.android.volley.Response;
import com.example.football.entity.Schedule;
import com.example.football.entity.Yuyue;
import com.example.football.util.JDBCUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;


public class MainActivity extends AppCompatActivity {

    public ArrayList<Yuyue> list;

    public String[] time;
    public String[] site;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        ActionBar actionBar=getSupportActionBar();
        if(actionBar!=null){
            actionBar.hide();
        }

        EditText account = findViewById(R.id.account);
        Intent intent=getIntent();
        String account1 = intent.getStringExtra("account");
        account.setText(account1);

        list = new ArrayList<>();
        time = new String[5];
        time[0] = "08:00-11:00";
        time[1] = "11:00-14:00";
        time[2] = "14:00-17:00";
        time[3] = "17:00-20:00";
        time[4] = "20:00-23:00";
        site = new String[3];
        site[0] = "西区足球场";
        site[1] = "中区足球场";
        site[2] = "东区足球场";


        /**************************************存入预约数据****************************************/

//        for(int i=0;i<10;i++){
//            for(int j =0;j<5;j++){
//                for(int z=0;z<3;z++){
//                    JSONObject jsonObject=new JSONObject();
//                    try {
//                        jsonObject.put("date",getOldDate(i));
//                        jsonObject.put("time",time[j]);
//                        jsonObject.put("site",site[z]);
//                    } catch (JSONException e) {
//                        e.printStackTrace();
//                    }
//                    String url="http://"+ JDBCUtils.ip +":9000/APPinsertyuyue";
//                    RequestQueue requestQueue= Volley.newRequestQueue(MainActivity.this);
//                    JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, url,jsonObject, new Response.Listener<JSONObject>() {
//                        @Override
//                        public void onResponse(JSONObject jsonObject) {
////                            Log.d("1","插入成功");
//                        }
//                    }, new Response.ErrorListener() {
//                        @Override
//                        public void onErrorResponse(VolleyError volleyError) {
//                            Toast.makeText(MainActivity.this, "网络出错", Toast.LENGTH_SHORT).show();
//                        }
//                    });
//                    requestQueue.add(jsonObjectRequest);
//                    try {
//                        Thread.sleep(100);
//                    } catch (InterruptedException e) {
//                        return;
//                    }
//                }
//            }
//        }

        /*********************************************8*******************************************/
}


    public static String getOldDate(int distanceDay) {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate = new Date();
        Calendar date = Calendar.getInstance();
        date.setTime(beginDate);
        date.set(Calendar.DATE, date.get(Calendar.DATE) + distanceDay);
        Date endDate = null;
        try {
            endDate = dft.parse(dft.format(date.getTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
//        Log.d("1",dft.format(endDate));
        return dft.format(endDate);
    }




    public void reg(View view){
        startActivity(new Intent(getApplicationContext(),RegisterActivity.class));
    }
    public void login(View view) {
        final EditText login_username = findViewById(R.id.account);
        final EditText login_password = findViewById(R.id.password);
        String usernameStr = login_username.getText().toString().trim();
        String passwordStr = login_password.getText().toString().trim();
        if(usernameStr.equals("")||passwordStr.equals("")){
            Toast.makeText(MainActivity.this, "用户名密码不能为空", Toast.LENGTH_SHORT).show();
        }else {
            JSONObject jsonObject=new JSONObject();
            try {
                jsonObject.put("account",usernameStr);
                jsonObject.put("password",passwordStr);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            String url="http://"+ JDBCUtils.ip +":9000/APPlogin";
            RequestQueue requestQueue= Volley.newRequestQueue(MainActivity.this);
            JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, url,jsonObject, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {
                    try {
                        Log.d("信息", jsonObject.toString());
                        String msg = jsonObject.getString("msg");
                        Log.d("msg", msg);
                        if(msg.equals("登录成功")){
                            JSONObject detail = jsonObject.getJSONObject("detail");
                            String account = detail.getString("account");
                            Integer id = detail.getInt("id");
                            String name = detail.getString("name");
                            String byteam = detail.getString("byteam");
                            Intent intent=new Intent(MainActivity.this,Main2Activity.class);
                            intent.putExtra("account",account);
                            intent.putExtra("id",id);
                            intent.putExtra("name",name);
                            intent.putExtra("byteam",byteam);
                            startActivity(intent);
                        }else if(msg.equals("用户名或密码错误")){
                            Toast.makeText(MainActivity.this, "用户名密码有误", Toast.LENGTH_SHORT).show();
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    Toast.makeText(MainActivity.this, "网络出错", Toast.LENGTH_SHORT).show();
                }
            });
            requestQueue.add(jsonObjectRequest);
        }

    }

}
